package com.googlecode.guiceservlet.pack;

import java.io.IOException;

import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;

import com.google.inject.servlet.GuiceFilter;

/**
 * Try to use this filter if you got jsp 404 error in tomcat 6,
 * check it here <a href="http://code.google.com/p/google-guice/issues/detail?id=372">the known issue</a>
 *
 */
public class JSPFixGuiceFilter extends GuiceFilter {
	
	@Override
	public void doFilter(ServletRequest request, ServletResponse response, FilterChain filterChain) 
		throws IOException, ServletException {
		request = new HttpServletRequestWrapper((HttpServletRequest) request) {
			@Override
			public Object getAttribute(String name) {
				if ("org.apache.catalina.jsp_file".equals(name)) {
					return super.getServletPath();
				}
				return super.getAttribute(name);
			}
		};

		super.doFilter(request, response, filterChain);
	}
}
